import MessageSenderUtils from '../libs/message_sender_utils.js';
import Service from '../libs/service_0_0_6.js';

let table;
chrome.devtools.panels.create("Panel", "icon.png", "../devtools/panel.html", function(panel) {
    panel.onShown.addListener( (extPanelWindow) => {
		table = extPanelWindow.document.querySelector('.container');
    });
});

chrome.runtime.onMessage.addListener(
	function (request, sender, sendResponse) {
		let inMsg = JSON.parse(request)
		if ((inMsg['sender'] === 'popup' || inMsg['sender'] === 'options')
				&& inMsg['recipient'] === utils.current) {
			utils.logInMessage(inMsg)
			let outMsg = new Object();
			outMsg.data = 'OK'
			outMsg.sender = utils.current
			outMsg.recipient = inMsg['sender']
			utils.log('Отправка ответа по адресу: ' + outMsg.recipient)
			utils.logOutMessage(outMsg)
			sendResponse( JSON.stringify(outMsg) );

			// let inMsgParse = inMsg['data']
			var li = Service.createCustomElement('li', `Сообщение от компонента '${inMsg['sender']}' с текстом: ${inMsg['data']}`, []);
			table.append(li);
		} else {
			utils.logIgnoreMessage(inMsg)
		}
	}
);

let utils = new MessageSenderUtils('devtools')